﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Xml;

namespace TestReatXML
{
    public class XMLDownloader
    {

        public class TrustAllCertificatePolicy : ICertificatePolicy
        {
            public TrustAllCertificatePolicy()
            {
            }

            public bool CheckValidationResult(ServicePoint sp,
                                              X509Certificate cert, WebRequest req, int problem)
            {
                return true;
            }
        }

        public static Dictionary<int, string> download(String certificate_path, String pwd, String url, String xml_output, DateTime time, StringWriter strWriter=null)
        {
            Dictionary<int, string> result = new Dictionary<int, string>();
            string fileName = time.ToString(SettingsValidator.date_format);
            Encoding encoding = Encoding.Default;
            System.Net.ServicePointManager.CertificatePolicy = new
                TrustAllCertificatePolicy();

            X509Certificate Cert =
                new X509Certificate2(certificate_path, pwd);
            HttpWebRequest tRequest = (HttpWebRequest)WebRequest.Create(url);
            tRequest.ClientCertificates.Add(Cert);
            ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;

            tRequest.UserAgent = "Client Cert Sample";
            tRequest.Method = "GET";
            HttpWebResponse Response = (HttpWebResponse)tRequest.GetResponse();
            if (strWriter == null)
                strWriter = new StringWriter();
            try
            {
                encoding = Encoding.GetEncoding(Response.CharacterSet);
            }
            catch (Exception)
            {
                throw;
            }
            using (StreamReader reader = new StreamReader(Response.GetResponseStream(), encoding))
            {
                string line;
                Boolean firstLine=true;
                while ((line = reader.ReadLine()) != null)
                {
                    if (firstLine)
                        strWriter.Write(line);
                    else
                        strWriter.WriteLine(line);
                }
            }
            System.IO.File.WriteAllText(xml_output + "\\" + fileName + ".xml", strWriter.ToString(), encoding);
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(strWriter.ToString());
            XmlNode errorCodes = xDoc.SelectSingleNode("/ErrorCodes");
            XmlNodeList errorCode = errorCodes.SelectNodes("ErrorCode");
            foreach (XmlNode node in errorCode)
            {

                int code = Convert.ToInt16(node.Attributes.GetNamedItem("code").Value);
                String txt = node.Attributes.GetNamedItem("text").Value;
                result.Add(code, txt);
            }
            return result;
        }
    }
}
